// pages/my/collection/list.js

const App = getApp()

Page({
  data: {
    collection: {
      items: [
    {
      name:'领有惠便利店',
      address:'北京市东城区北新桥',
      isDefault:0,
      icon:'../../../image/u2956.png'
    },
    {
      name: '乐购乐',
      address: '北京市东城区北新桥',
      isDefault: 1,
      icon: '../../../image/u2956.png'
    },
    {
      name: '土地土',
      address: '北京市东城区哈哈哈',
      isDefault: 1,
      icon: '../../../image/u2956.png'
    }
  ],
    params: {
        page: 1,
      limit: 10,
      },
    paginate: { total:2}
    },

    prompt: {
      hidden: !0,
      icon: '../../../image/empty.png',
      title: '您还没有收藏店铺！',
      text: '赶快去收藏哦',
    },
  },

    onLoad() {
      //this.collection = App.HttpResource('/collection/:id', {id: '@id'})
      //this.onPullDownRefresh()
    },

    initData() {
      this.setData({
        collection: {
          items: [],
          params: {
            page: 1,
            limit: 10,
          },
          paginate: {}
        }
      })
    },



    getList() {
      const collection = this.data.collection
      const params = collection.params

      // // App.HttpService.getCollectionList(params)
      this.collection.queryAsync(params)
        .then(res => {
          const data = res.data
          console.log(data)
          if (data.meta.code == 0) {
            collection.items = [...collection.items, ...data.data.items]
            collection.paginate = data.data.paginate
            collection.params.page = data.data.paginate.next
            collection.params.limit = data.data.paginate.perPage
            this.setData({
              collection: collection,
              'prompt.hidden': collection.items.length,
            })
          }
        })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
      console.info('onPullDownRefresh')
      this.initData()
      this.getList()
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
      console.info('onReachBottom')
      if (!this.data.collection.paginate.hasNext) return
      this.getList()
    },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})